﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Reflection;
using SGCG.Model;
using System.Windows.Data;
using System.ComponentModel;

namespace SGCG
{
    public partial class BorderOptions : UserControl, INotifyPropertyChanged
    {
        private IHasBorder __iHasBorder;

        public BorderOptions()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        private IEnumerable<Color> _colors = typeof(Colors)
            .GetProperties(BindingFlags.Public | BindingFlags.Static)
            .Select(property => (Color)property.GetValue(null, null));

        public IEnumerable<Color> Colors
        {
            get { return _colors; }
        }

        public IEnumerable<LineThickness> ValoresEspessura
        {
            get { return new[] { LineThickness.Normal, LineThickness.Dupla }; }
        }

        public IEnumerable<LineStyle> ValoresEstilo
        {
            get { return new[] { LineStyle.Normal, LineStyle.Pontilhado, LineStyle.Tracejado }; }
        }

        public IEnumerable<FillType> ValoresPreenchimento
        {
            get { return new[] { FillType.None, FillType.Solid, FillType.Cesar, FillType.Dragon }; }
        }

        public IEnumerable<ArrowType> ValoresSetas
        {
            get { return new[] { ArrowType.Nenhum, ArrowType.PontaA, ArrowType.PontaB, ArrowType.DuasPontas }; }
        }

        //public IHasBorder IHasBorder
        //{
        //    get { return this.__iHasBorder; }

        //    set
        //    {
        //        this.__iHasBorder = value ?? new Line(null)
        //        {
        //            LineThickness = (LineThickness)(this.LineThicknessList.SelectedItem ?? LineThickness.Normal),
        //            LineStyle = (LineStyle)(this.LineStyleList.SelectedItem ?? LineStyle.Normal),
        //            Color = (Color)(this.ColorsList.SelectedItem ?? System.Windows.Media.Colors.Black)
        //        };

        //        this.__iHasBorder.LineThickness = (LineThickness)(this.LineThicknessList.SelectedItem ?? this.__iHasBorder.LineThickness);
        //        this.__iHasBorder.LineStyle = (LineStyle)(this.LineStyleList.SelectedItem ?? this.__iHasBorder.LineStyle);
        //        this.__iHasBorder.Color = (Color)(this.ColorsList.SelectedItem ?? this.__iHasBorder.Color);

        //        if (value is IFillable)
        //            IFillable = value as IFillable;

        //        if (value is IHasArrow)
        //            IHasArrow = value as IHasArrow;

        //        if (this.PropertyChanged != null)
        //        {
        //            this.PropertyChanged(this, new PropertyChangedEventArgs("IHasBorder"));
        //        }
        //    }
        //}
        public IHasBorder IHasBorder
        {
            get { return this.__iHasBorder; }

            set
            {
                if (value != null)
                {
                    value.LineThickness = (LineThickness)(LineThicknessList.SelectedItem ?? value.LineThickness);
                    value.LineStyle = (LineStyle)(LineStyleList.SelectedItem ?? value.LineStyle);
                    value.Color = (Color)(ColorsList.SelectedItem ?? value.Color);
                }

                if (value is IFillable)
                    IFillable = value as IFillable;

                if (value is IHasArrow)
                    IHasArrow = value as IHasArrow;

                this.__iHasBorder = value;

                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs("IHasBorder"));
                }
            }
        }

        public IFillable IFillable
        {
            get { return IHasBorder as IFillable; }

            set
            {
                if (value != null)
                {
                    value.Fill = (FillType)(FillList.SelectedItem ?? value.Fill);
                    value.FillColor = (Color)(FillColorsList.SelectedItem ?? value.FillColor);
                }

                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs("IFillable"));
                }
            }
        }

        public IHasArrow IHasArrow
        {
            get { return IHasBorder as IHasArrow; }

            set
            {
                if (value != null)
                {
                    value.ArrowType = (ArrowType)(ArrowTypeList.SelectedItem ?? value.ArrowType);
                }

                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs("IHasArrow"));
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

    public sealed class ColorToStringConverter : IValueConverter
    {
        object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            PropertyInfo property =
                typeof(Colors).GetProperties(BindingFlags.Public | BindingFlags.Static)
                     .SingleOrDefault(p => ((Color)p.GetValue(null, null)).Equals((Color)value));

            if (property == null)
            {
                return value.ToString();
            }
            else
            {
                return property.Name;
            }
        }

        object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
